package ks.client.parser;

import heineman.Idiot;

import java.util.StringTokenizer;

import javax.swing.JFrame;

import org.w3c.dom.Document;

import arch.ClientArchitecture;

import ks.client.UserContext;
import ks.client.gamefactory.GameWindow;
import ks.client.gui.GMGUI;
import ks.client.lobby.Lobby;
import ks.framework.common.Message;
import ks.launcher.Main;
import ks.managers.table.TableManager;

/**
 * Agent responsible for converting text-based programmer requests into
 * actual XML Messages to be delivered to the server.
 * <p>
 * Note that this HACK will need to be replaced with a better design as more 
 * sophisticated commands are implemented on your system.
 * 
 * @author George Heineman
 */
public class CommandParser {

	/** All commands are tagged with unique counter. */
	static int ctr;
	
	/**
	 * Processes given string to see if it contains the valid encoding
	 * of a command to be delivered to the server.
	 * <p>
	 * Because this method "could" be used by multiple client-side threads,
	 * we have locked it up using the synchronized keyword.  
	 * <p>
	 * Clients should in no way take advantage of the fact that {@link #ctr} is 
	 * monotonically increasing, since there could be gaps.
	 * 
	 * @param s
	 * @return message constructed from the string or <code>null</code> if 
	 *         unable to interpret s.
	 */
	public synchronized static Message process (String s) {
		if (!s.startsWith("/")) {
			return null;
		}

		// trim away and continue
		s = s.substring(1);
		
		// HACK: trying to show simple game start
		if (s.equalsIgnoreCase("idiot")) {
			// do it!
			GameWindow gw = Main.generateWindow(new Idiot(), 123);
			gw.setVisible(true);
			return null;
		}


		String prefix = "<command version='1.0' id='" + (++ctr) + "'>\n"; 

		StringTokenizer st = new StringTokenizer(s);
		String cmd = st.nextToken();
		if (cmd.equals ("join")) {
			String tbl = st.nextToken();
			String msg = "<join table=\"" + tbl + "\"/>";
			Document d = Message.construct(prefix+msg+"</command>");
			return new Message(d);
		} else if (cmd.equals ("accept")) {
			String tbl = st.nextToken();
			String ply = st.nextToken();
			String msg = "<accept table=\"" + tbl + "\"" +
			" player=\"" + ply + "\"" +
			"/>";
			Document d = Message.construct(prefix+msg+"</command>");
			return new Message(d);
		} else if (cmd.equals ("reject")) {
			String tbl = st.nextToken();
			String ply = st.nextToken();
			String msg = "<reject table=\"" + tbl + "\"" +
			" player=\"" + ply + "\"" +
			"/>";
			Document d = Message.construct(prefix+msg+"</command>");
			return new Message(d);
		} else if (cmd.equals ("user-kick")) {
			prefix = "<admin-command version='1.0' id='" + (++ctr) + "'>\n"; 

			String msg = "<user-kick>";
			while (st.hasMoreTokens()) {
				String ply = st.nextToken();
				msg += "<playername name=\"" + ply + "\"/>";
			}

			Document d = Message.construct(prefix+msg+"</user-kick></admin-command>");
			return new Message(d);
		} else if (cmd.equals ("gmgui")) {
			UserContext context = ClientArchitecture.solo.getUserContext();
			TableManager tm = context.getTableManager();
			
			ClientArchitecture.solo.newGMGUI(7);
			JFrame g 
			= ClientArchitecture.solo.getGMGUI(7);
			
			g.setVisible(true);
			return null;
		}

// BEGIN-EXAMPLE
		// This is just a HACK to get things done without having much of a GUI
		// process commands: 'icon-list' and 'icon-add'
		if (cmd.equals ("icon-add")) {
			// create the command to send to the server
			// icon-add STRING URL
			String name = st.nextToken();
			String url = st.nextToken();
			prefix = "<admin-command version='1.0' id='" + (++ctr) + "'>\n"; 

			String msg = "<icon-add icon=\"" + name + "\" uri=\"" + url + "\"/>";
			Document d = Message.construct(prefix+msg+"</admin-command>");
			return new Message(d);
		} else if (cmd.equals ("icon-list")) {
			// create the command to send to the server
			// icon-list
			Document d = Message.construct("<command version='1.0' id='" + (++ctr) + "'><icon-list/></command>");
			return new Message(d);
		}
		
		// This is just a HACK to get things done without having much of a GUI
		// process commands: 'icon-list' and 'icon-add'
		if (cmd.equals ("plugin-install")) {
			// create the command to send to the server
			// icon-add STRING URL
			
			String url = st.nextToken();
			prefix = "<admin-command version='1.0' id='" + (++ctr) + "'>\n"; 

			String msg = "<plugin-install uri=\"" + url + "\"/>";
			Document d = Message.construct(prefix+msg+"</admin-command>");
			return new Message(d);
		} else if (cmd.equals ("plugin-list")) {
			// create the command to send to the server
			// icon-list
			Document d = Message.construct("<command version='1.0' id='" + (++ctr) + "'><plugin-list/></command>");
			return new Message(d);
		}
		
		
// END-EXAMPLE
		
		// no idea what to do with given input.
		return null;		
	}
}
